package com.lefu.es.system;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.model.TImage;
import com.jph.takephoto.model.TResult;
import com.lefu.es.constant.ActivityVolues;
import com.lefu.es.constant.UtilConstants;
import com.lefu.es.entity.UserModel;
import com.lefu.es.event.ReFlushBabyEvent;
import com.lefu.es.service.UserService;
import com.lefu.es.util.CustomHelper;
import com.lefu.es.util.ImageUtil;
import com.lefu.es.util.LogUtils;
import com.lefu.es.util.SharedPreferencesUtil;
import com.lefu.es.util.ToastUtils;
import com.lefu.es.util.Tool;
import com.lefu.es.util.UtilTooth;
import com.lefu.es.wheelview.ScreenInfo;
import com.lefu.es.wheelview.WheelMain;
import com.wellness.scale.R;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 编辑用户信息
 *
 * @author lfl
 */
public class BabyUserEditActivity extends TakePhotoActivity {
    private Button maleBtn;
    private Button femaleBtn;

    private TextView ordinaryBtn;
    private TextView amatuerBtn;
    private TextView professBtn;

    private ImageView imageView6;
    private ImageView iv_amateur;
    private ImageView iv_yundong;


    private String userType;

    private TextView imageCancel;
    private TextView imageSave;

    private EditText nameET;
    private EditText heightET, heightET2;
    private TextView ageET;
    private EditText monthET;
    private EditText idET;

    private RadioGroup group;
    private TextView heng_tv;
    private TextView danwei_tv;

    private RadioGroup unitgroup;
    private RadioButton kgrb, strb, lbrb;

    private boolean isKG = true;
    private UserService uservice;
    private SharedPreferencesUtil su = new SharedPreferencesUtil(this);

    private LinearLayout taget_layout = null;
    private EditText target_edittv = null;
    private EditText target_edittv2 = null;
    private TextView heng_tv2;
    private TextView target_danwei_tv;
    private String TAG = "UserEditActivity";

    /**
     * 修改头像
     */
    private SimpleDraweeView ib_upphoto;
    /**
     * 修改头像自定义Dialog中的按钮
     **/
    private Button rb_dialog[] = new Button[3];
    private int RadioButtonID[] = {R.id.rb_setPhoto1, R.id.rb_setPhoto2, R.id.rb_setPhoto3};
    private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
    private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
    private static final int PHOTO_REQUEST_CUT = 3;// 结果
    // 创建一个以当前时间为名称的文件

    String path = Environment.getExternalStorageDirectory() + "/brithPhoto/";

    String photoImg = "";

    /**
     * 通过centerIndex来决定采用那种存储方式
     **/
    private int centerIndex;

    // 获得编辑权限
    SharedPreferences.Editor editor;

    /* 头像名称 */
    private static final String PHOTO_FILE_NAME = "temp_photo.jpg";
    private File tempFile;
    private Bitmap bitmap;

    /*点击选择年龄弹窗*/
    private long ageClickTime = System.currentTimeMillis();

    private RadioButton sex_male_checkbox, sex_female_checkbox;

    private RadioButton ordinary_btn_checkbox, amateur_btn_checkbox, profess_btn_checkbox;

    // 拍照临时图片
    private String mTempPhotoPath;
    // 剪切后图像文件
    private Uri mDestinationUri;

    protected UserModel user = null; //需要编辑的用户

    protected boolean isFirst = false;
    private CustomHelper customHelper;


    public static Intent creatIntent(Context context, UserModel user, boolean isfirst) {
        Intent intent = new Intent(context, BabyUserEditActivity.class);
        intent.putExtra("user", user);
        intent.putExtra("isfirst", isfirst);
        return intent;
    }

    public void showAlertDailog(String title) {
        new com.lefu.es.view.AlertDialog(BabyUserEditActivity.this).builder()
                .setTitle(getResources().getString(R.string.waring_title))
                .setMsg(title)
                .setPositiveButton(getResources().getString(R.string.ok_btn), new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                }).show();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Serializable serializable = getIntent().getSerializableExtra("user");
        isFirst = getIntent().getBooleanExtra("isfirst", false);
        if (null != serializable) {
            user = (UserModel) serializable;
        } else {
            user = UtilConstants.CURRENT_USER;
            if (null == user) {
                try {
                    UtilConstants.CURRENT_USER = JSONObject.parseObject((String) UtilConstants.su.readbackUp("lefuconfig", "addUser", ""), UserModel.class);
                    user = UtilConstants.CURRENT_USER;
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
        if (null == user) {
            ToastUtils.ToastCenter(BabyUserEditActivity.this, "获取用户信息失败");
            finish();
            return;
        }
        if ("P999".equals(user.getGroup())) {
            setContentView(R.layout.activity_baby_edit);
        } else {
            setContentView(R.layout.activity_user_edit);
        }
        customHelper = CustomHelper.of();
        ButterKnife.bind(this);
        mDestinationUri = Uri.fromFile(new File(this.getCacheDir(), "cropImage.jpeg"));
        mTempPhotoPath = Environment.getExternalStorageDirectory() + File.separator + "photo.jpeg";
        SharedPreferences sharedPreferences = getSharedPreferences(ActivityVolues.shape_name, MODE_PRIVATE);
        editor = sharedPreferences.edit();
        Intent center = getIntent();
        centerIndex = center.getIntExtra("center", -100);

        //ExitApplication.getInstance().addActivity(this);

		/*判断用户数据是否被清空*/
        UtilConstants.su = new SharedPreferencesUtil(BabyUserEditActivity.this);
        /*新检测添加*/
        if (UtilConstants.CURRENT_USER == null) {
            //AppData.isCheckScale=true;
            UtilConstants.CURRENT_USER = JSONObject.parseObject((String) UtilConstants.su.readbackUp("lefuconfig", "addUser", ""), UserModel.class);
        }

        initView();
    }

    @SuppressWarnings("deprecation")
    private void initView() {
        uservice = new UserService(this);
        imageView6 = (ImageView) findViewById(R.id.imageView6);
        iv_amateur = (ImageView) findViewById(R.id.iv_amateur);
        iv_yundong = (ImageView) findViewById(R.id.iv_yundong);
        nameET = (EditText) findViewById(R.id.add_brith_name);
        heightET = (EditText) findViewById(R.id.add_brith_height1);
        heightET2 = (EditText) findViewById(R.id.add_brith_height2);
        ageET = (TextView) findViewById(R.id.add_brith_day);
        taget_layout = (LinearLayout) this.findViewById(R.id.taget_layout);
        target_edittv = (EditText) this.findViewById(R.id.target_edittv);
        target_edittv2 = (EditText) this.findViewById(R.id.target_edittv2);

        sex_male_checkbox = (RadioButton) this.findViewById(R.id.sex_male_checkbox);
        sex_female_checkbox = (RadioButton) this.findViewById(R.id.sex_female_checkbox);

        ordinary_btn_checkbox = (RadioButton) findViewById(R.id.ordinary_btn_checkbox);
        amateur_btn_checkbox = (RadioButton) findViewById(R.id.amateur_btn_checkbox);
        profess_btn_checkbox = (RadioButton) findViewById(R.id.profess_btn_checkbox);

        if (UtilConstants.BATHROOM_SCALE.equals(UtilConstants.CURRENT_SCALE)) {
            taget_layout.setVisibility(View.VISIBLE);
        }
        heng_tv = (TextView) this.findViewById(R.id.heng);
        heng_tv2 = (TextView) this.findViewById(R.id.kgheng);
        danwei_tv = (TextView) this.findViewById(R.id.add_height_danwei);
        target_danwei_tv = (TextView) this.findViewById(R.id.add_taeget_danwei);
        unitgroup = (RadioGroup) this.findViewById(R.id.unit_radioGourp);
        kgrb = (RadioButton) this.findViewById(R.id.add_unit_kg);
        strb = (RadioButton) this.findViewById(R.id.add_unit_ST);
        lbrb = (RadioButton) this.findViewById(R.id.add_unit_lb);
        kgrb.setOnClickListener(kgOnClickListener);
        strb.setOnClickListener(kgOnClickListener);
        lbrb.setOnClickListener(kgOnClickListener);

        maleBtn = (Button) findViewById(R.id.sex_male);
        femaleBtn = (Button) findViewById(R.id.sex_female);
        ordinaryBtn = (TextView) findViewById(R.id.ordinary_btn);
        amatuerBtn = (TextView) findViewById(R.id.amateur_btn);
        professBtn = (TextView) findViewById(R.id.profess_btn);
        maleBtn.setOnClickListener(sexOnClickListener);
        femaleBtn.setOnClickListener(sexOnClickListener);
        //ordinaryBtn.setOnClickListener(leverOnClickListener);
        //amatuerBtn.setOnClickListener(leverOnClickListener);
        //professBtn.setOnClickListener(leverOnClickListener);
        imageCancel = (TextView) findViewById(R.id.userCancel);
        imageSave = (TextView) findViewById(R.id.userSave);
        imageCancel.setOnClickListener(imgOnClickListener);
        imageSave.setOnClickListener(imgOnClickListener);
        //ageET.setOnClickListener(imgOnClickListener);

        ib_upphoto = (SimpleDraweeView) this.findViewById(R.id.reviseHead);
        ib_upphoto.setOnClickListener(photoClickListener);

        UtilConstants.CHOICE_KG = UtilConstants.UNIT_KG;
        if (null != user) {
            UtilConstants.CHOICE_KG = user.getDanwei();
            if (UtilConstants.CHOICE_KG.equals(UtilConstants.UNIT_LB) || UtilConstants.CHOICE_KG.equals(UtilConstants.UNIT_ST) || UtilConstants.CHOICE_KG.equals(UtilConstants.UNIT_FATLB)) {

            } else {
                UtilConstants.CHOICE_KG = UtilConstants.UNIT_KG;
            }
            this.nameET.setText(user.getUserName());
            if (TextUtils.isEmpty(user.getPer_photo())) {
                if (null != user.getSex() && user.getSex().equals("0")) {
                  /*  if (TextUtils.isEmpty(photoImg)) {
                        ib_upphoto.setImageResource(R.drawable.baby_default);
                    }*/
                    femaleBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.womend));
                } else {
                 /*   if (TextUtils.isEmpty(photoImg)) {
                        ib_upphoto.setImageResource(R.drawable.baby_default);
                    }*/
                    maleBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.mand));
                }

            } else {
                photoImg = user.getPer_photo();
                ib_upphoto.setImageURI(Uri.fromFile(new File(user.getPer_photo())));
            }

            if (UtilConstants.CHOICE_KG.equals(UtilConstants.UNIT_ST)) {
                isKG = false;
                strb.setChecked(true);
                heightET2.setVisibility(View.VISIBLE);
                heng_tv.setVisibility(View.VISIBLE);
                danwei_tv.setText(this.getText(R.string.ftin_danwei));

                target_edittv2.setVisibility(View.VISIBLE);
                heng_tv2.setVisibility(View.VISIBLE);
                target_danwei_tv.setText(this.getText(R.string.lb_danwei));
            } else if (UtilConstants.CHOICE_KG.equals(UtilConstants.UNIT_LB) || UtilConstants.CHOICE_KG.equals(UtilConstants.UNIT_FATLB)) {
                isKG = false;
                lbrb.setChecked(true);
                heightET2.setVisibility(View.VISIBLE);
                heng_tv.setVisibility(View.VISIBLE);
                danwei_tv.setText(this.getText(R.string.ftin_danwei));

                target_edittv2.setVisibility(View.VISIBLE);
                heng_tv2.setVisibility(View.VISIBLE);
                target_danwei_tv.setText(this.getText(R.string.lb_danwei));
            } else {
                kgrb.setChecked(true);
                heightET2.setVisibility(View.GONE);
                heng_tv.setVisibility(View.GONE);
                danwei_tv.setText(this.getText(R.string.cm_danwei));

                target_edittv2.setVisibility(View.GONE);
                heng_tv2.setVisibility(View.GONE);
                target_danwei_tv.setText(this.getText(R.string.kg_danwei));
            }

            if (null != user) {
                this.org_hei = user.getBheigth();
                this.org_hei1 = UtilTooth.cm2FT_INArray(user.getBheigth())[0];
                this.org_hei2 = UtilTooth.cm2FT_INArray(user.getBheigth())[0];
            }
            this.targ_old = user.getTargweight();
            this.targ_new = this.targ_old;
            if (isKG) {
                heightET.setText((int) user.getBheigth() + "");
                target_edittv.setText(UtilTooth.toOnePonit(user.getTargweight()) + "");
            } else {
                heightET.setText(UtilTooth.cm2FT_INArray(user.getBheigth())[0]);
                heightET2.setText(UtilTooth.cm2FT_INArray(user.getBheigth())[1]);

                target_edittv2.setVisibility(View.GONE);
                heng_tv2.setVisibility(View.GONE);
                String wei = target_edittv.getText().toString().trim();
                if (null == wei || "".equals(wei)) {
                    wei = "0";
                }
                float ftin = UtilTooth.kgToLB_target(user.getTargweight());
                target_edittv.setText(UtilTooth.onePoint(ftin) + "");
            }

            ageET.setText(user.getBirth());

            if ("1".equals(user.getSex())) {
                maleBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.mand));
                femaleBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.women));
                sex = "1";
                sex_male_checkbox.setChecked(true);
                sex_female_checkbox.setChecked(false);
            } else {
                maleBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.man));
                femaleBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.womend));
                sex = "0";
                sex_male_checkbox.setChecked(false);
                sex_female_checkbox.setChecked(true);
            }

            if ("1".equals(user.getLevel())) {
                userType = "1";
                this.iv_amateur.setImageResource(R.drawable.pyeyud);
                ordinary_btn_checkbox.setChecked(false);
                amateur_btn_checkbox.setChecked(true);
                profess_btn_checkbox.setChecked(false);
            } else if ("2".equals(user.getLevel())) {
                userType = "2";
                iv_yundong.setImageResource(
                        R.drawable.pyundongd);
                ordinary_btn_checkbox.setChecked(false);
                amateur_btn_checkbox.setChecked(false);
                profess_btn_checkbox.setChecked(true);
            } else {
                userType = "0";
                this.imageView6.setImageResource(R.drawable.pcommd);
                ordinary_btn_checkbox.setChecked(true);
                amateur_btn_checkbox.setChecked(false);
                profess_btn_checkbox.setChecked(false);
            }
        }

        ageET.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                /*增加时延，防止快速点击两次*/
                if ((System.currentTimeMillis() - ageClickTime) > 1000) {
                    showDateTimePicker();
                }
                ageClickTime = System.currentTimeMillis();
                return false;
            }
        });
        sex_male_checkbox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                sex = "1";
               /* if (TextUtils.isEmpty(photoImg)) {
                    ib_upphoto.setImageResource(R.drawable.nan);
                }*/
                maleBtn.setBackgroundDrawable(getResources().getDrawable(
                        R.drawable.mand));
                femaleBtn.setBackgroundDrawable(getResources().getDrawable(
                        R.drawable.women));

                sex_male_checkbox.setChecked(true);
                sex_female_checkbox.setChecked(false);

            }
        });

        sex_female_checkbox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                sex = "0";
              /*  if (TextUtils.isEmpty(photoImg)) {
                    ib_upphoto.setImageResource(R.drawable.nu);
                }*/
                maleBtn.setBackgroundDrawable(getResources().getDrawable(
                        R.drawable.man));
                femaleBtn.setBackgroundDrawable(getResources().getDrawable(
                        R.drawable.womend));
                sex_male_checkbox.setChecked(false);
                sex_female_checkbox.setChecked(true);
            }
        });

        /*ordinary_btn_checkbox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                userType = "0";
                imageView6.setImageResource(
                        R.drawable.pcommd);
                iv_amateur.setImageResource(
                        R.drawable.pyeyu);
                iv_yundong.setImageResource(
                        R.drawable.pyundong);

                ordinary_btn_checkbox.setChecked(true);
                amateur_btn_checkbox.setChecked(false);
                profess_btn_checkbox.setChecked(false);
                showAlertDialog(getResources().getString(R.string.putong_waring_title));
            }
        });*/

/*
        amateur_btn_checkbox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                userType = "1";
                imageView6.setImageResource(
                        R.drawable.pcomm);
                iv_amateur.setImageResource(
                        R.drawable.pyeyud);
                iv_yundong.setImageResource(
                        R.drawable.pyundong);

                ordinary_btn_checkbox.setChecked(false);
                amateur_btn_checkbox.setChecked(true);
                profess_btn_checkbox.setChecked(false);
                showAlertDialog(getResources().getString(R.string.yeyu_waring_title));
            }
        });
*/


       /* profess_btn_checkbox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                userType = "2";
                imageView6.setImageResource(R.drawable.pcomm);
                iv_amateur.setImageResource(R.drawable.pyeyu);
                iv_yundong.setImageResource(R.drawable.pyundongd);

                ordinary_btn_checkbox.setChecked(false);
                amateur_btn_checkbox.setChecked(false);
                profess_btn_checkbox.setChecked(true);
                showAlertDialog(getResources().getString(R.string.zhuanye_waring_title));
            }
        });*/
    }


    protected static final int REQUEST_STORAGE_READ_ACCESS_PERMISSION = 101;
    protected static final int REQUEST_STORAGE_WRITE_ACCESS_PERMISSION = 102;
    protected static final int REQUEST_CAMERA_PERMISSION = 103;


    /**
     * 请求权限
     * <p>
     * 如果权限被拒绝过，则提示用户需要权限
     */
    protected void requestPermission(final String permission, String rationale, final int requestCode) {
        if (shouldShowRequestPermissionRationale(permission)) {
            showAlertDialog(getString(R.string.permission_title_rationale), rationale,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            requestPermissions(new String[]{permission}, requestCode);
                        }
                    }, getString(R.string.ok_btn), null, getString(R.string.cancle_btn));
        } else {
            requestPermissions(new String[]{permission}, requestCode);
        }
    }

    private AlertDialog mAlertDialog;

    /**
     * 显示指定标题和信息的对话框
     *
     * @param title                         - 标题
     * @param message                       - 信息
     * @param onPositiveButtonClickListener - 肯定按钮监听
     * @param positiveText                  - 肯定按钮信息
     * @param onNegativeButtonClickListener - 否定按钮监听
     * @param negativeText                  - 否定按钮信息
     */
    protected void showAlertDialog(@Nullable String title, @Nullable String message,
                                   @Nullable DialogInterface.OnClickListener onPositiveButtonClickListener,
                                   @NonNull String positiveText,
                                   @Nullable DialogInterface.OnClickListener onNegativeButtonClickListener,
                                   @NonNull String negativeText) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(positiveText, onPositiveButtonClickListener);
        builder.setNegativeButton(negativeText, onNegativeButtonClickListener);
        mAlertDialog = builder.show();
    }


    private boolean hasSdcard() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void onPhotosReturned(List<File> photos) {
        if (null != photos && photos.size() > 0) {
            photoImg = photos.get(0).getAbsolutePath();
            LogUtils.i("UserAdd", "photoImg:" + photoImg);
            String urlStr = ImageUtil.compressPicture1(photoImg);
            LogUtils.i("UserAdd", "urlStr:" + urlStr);
            ib_upphoto.setImageURI(Uri.fromFile(new File(urlStr)));
        }
    }

    @Override
    protected void onDestroy() {
        // Clear any configuration that was done!
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    /**
     * 剪切图片
     */
    private void crop(Uri uri) {
        // 裁剪图片意图
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // 裁剪框的比例，1：1
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // 裁剪后输出图片的尺寸大小
        intent.putExtra("outputX", 100);
        intent.putExtra("outputY", 100);
        // 图片格式
        intent.putExtra("outputFormat", "png");
        intent.putExtra("noFaceDetection", true);// 取消人脸识别
        intent.putExtra("return-data", true);// true:不返回uri，false：返回uri
        startActivityForResult(intent, PHOTO_REQUEST_CUT);
    }

    private AlertDialog alertDialog;

    /**
     * 显示设置头像的Dialog
     */
    private void showSetPhotoDialog() {
        // 初始化自定义布局参数
        LayoutInflater layoutInflater = getLayoutInflater();
        // 为了能在下面的OnClickListener中获取布局上组件的数据，必须定义为final类型.
        View customLayout = layoutInflater.inflate(R.layout.showsetphototdialog, (ViewGroup) findViewById(R.id.customDialog));

        for (int i = 0; i < rb_dialog.length; i++) {
            rb_dialog[i] = (Button) customLayout.findViewById(RadioButtonID[i]);
            rb_dialog[i].setOnClickListener(photoClickListener);
        }

        alertDialog = new AlertDialog.Builder(this).setView(customLayout).show();

        Window window = alertDialog.getWindow();
        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
        window.setWindowAnimations(R.style.mystyle); // 添加动画
    }

    private float org_hei = 0.0f;
    private String org_hei1 = "0";
    private String org_hei2 = "0";
    private float targ_old = 0.0f;
    private float targ_new = 0.0f;
    private float targetweight = 0;
    private float targetweightLB = 0;
    OnClickListener kgOnClickListener = new OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {
                case (R.id.add_unit_kg):
                    LogUtils.d(TAG, "===============上一个单位是：" + UtilConstants.CHOICE_KG);
                    heightET.setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});
                    if (heightET2.getVisibility() == View.VISIBLE) {
                        heightET2.setVisibility(View.GONE);
                        heng_tv.setVisibility(View.GONE);
                        String h1 = heightET.getText().toString();
                        if ("".equals(h1.trim()) || "0.0".equals(h1.trim()))
                            h1 = "0";
                        String h2 = heightET2.getText().toString();
                        if ("".equals(h2.trim()))
                            h2 = "0";
                        if (h1.equals(org_hei1) && h2.equals(org_hei2)) {
                            heightET.setText((int) org_hei + "");
                        } else {
                            org_hei1 = h1;
                            org_hei2 = h2;
                            String[] ftin = {h1, h2};
                            int cm = UtilTooth.ft_in2CMArray(ftin);
                            heightET.setText(cm + "");
                            org_hei = cm;
                        }
                    }
                    if (UtilConstants.CHOICE_KG.equals(UtilConstants.UNIT_LB) || UtilConstants.CHOICE_KG.equals(UtilConstants.UNIT_ST) || "lb:oz".equals(UtilConstants.CHOICE_KG)) {
                        target_edittv2.setVisibility(View.GONE);
                        heng_tv2.setVisibility(View.GONE);
                        String h1 = target_edittv.getText().toString();
                        if (null == h1 || "".equals(h1.trim()))
                            h1 = "0";
                        String kg = "0";
                        targetweight = UtilTooth.lbToKg_target(Float.parseFloat(h1));
                        LogUtils.d(TAG, "点KG后的目标体重：" + targetweight);
                        kg = UtilTooth.onePoint(targetweight);
                        LogUtils.d(TAG, "点KG后的目标体重1：" + kg);
                        target_edittv.setText(kg);
                    }

                    isKG = true;
                    UtilConstants.CHOICE_KG = UtilConstants.UNIT_KG;
                    danwei_tv.setText("cm");
                    target_danwei_tv.setText(UtilConstants.UNIT_KG);
                    break;
                case (R.id.add_unit_ST):
                    heightET.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1)});
                    if (heightET2.getVisibility() == View.GONE) {
                        heightET2.setVisibility(View.VISIBLE);
                        heng_tv.setVisibility(View.VISIBLE);
                        if (!"".equals(heightET.getText().toString())) {
                            org_hei = Float.parseFloat(heightET.getText().toString().trim());
                            String[] ftin = UtilTooth.cm2FT_INArray(Float.parseFloat(heightET.getText().toString()));
                            if (null != ftin) {
                                org_hei1 = ftin[0];
                                org_hei2 = ftin[1];
                                heightET.setText(ftin[0]);
                                heightET2.setText(ftin[1]);
                            }
                        }

                    }
                    if (UtilConstants.CHOICE_KG.equals(UtilConstants.UNIT_LB)) {
                        target_edittv2.setVisibility(View.GONE);
                        heng_tv2.setVisibility(View.GONE);
                    } else if (UtilConstants.CHOICE_KG.equals(UtilConstants.UNIT_KG)) {
                        target_edittv2.setVisibility(View.GONE);
                        heng_tv2.setVisibility(View.GONE);
                        String h1 = target_edittv.getText().toString();
                        if (null == h1 || "".equals(h1.trim()))
                            h1 = "0";
                        String kg = "0";
                        if ("0".equals(h1)) {
                            kg = "0";
                        } else {
                            targetweightLB = UtilTooth.kgToLB_target(Float.parseFloat(h1));
                            kg = UtilTooth.onePoint(targetweightLB);
                        }
                        if (!"0".equals(kg) || !"0.0".equals(kg)) target_edittv.setText(kg);
                        target_edittv2.setText("");
                    }
                    isKG = false;
                    UtilConstants.CHOICE_KG = UtilConstants.UNIT_ST;
                    danwei_tv.setText("ft:in");
                    target_danwei_tv.setText(UtilConstants.UNIT_LB);
                    break;
                case (R.id.add_unit_lb):
                    heightET.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1)});
                    if (heightET2.getVisibility() == View.GONE) {
                        heightET2.setVisibility(View.VISIBLE);
                        heng_tv.setVisibility(View.VISIBLE);
                        if (!"".equals(heightET.getText().toString())) {
                            org_hei = Float.parseFloat(heightET.getText().toString().trim());
                            String[] ftin = UtilTooth.cm2FT_INArray(Float.parseFloat(heightET.getText().toString()));
                            if (null != ftin) {
                                org_hei1 = ftin[0];
                                org_hei2 = ftin[1];
                                heightET.setText(ftin[0]);
                                heightET2.setText(ftin[1]);
                            }
                        }
                    }
                    if (UtilConstants.CHOICE_KG.equals(UtilConstants.UNIT_ST)) {
                        target_edittv2.setVisibility(View.GONE);
                        heng_tv2.setVisibility(View.GONE);
                    } else if (UtilConstants.CHOICE_KG.equals(UtilConstants.UNIT_KG)) {
                        target_edittv2.setVisibility(View.GONE);
                        heng_tv2.setVisibility(View.GONE);
                        String h1 = target_edittv.getText().toString();
                        if (null == h1 || "".equals(h1.trim()))
                            h1 = "0";
                        String kg = "0";
                        if ("0".equals(h1)) {
                            kg = "0";
                        } else {
                            targetweightLB = UtilTooth.kgToLB_target(Float.parseFloat(h1));
                            kg = UtilTooth.onePoint(targetweightLB);
                        }
                        if (!"0".equals(kg) || !"0.0".equals(kg)) target_edittv.setText(kg);
                        target_edittv2.setText("");
                    }
                    isKG = false;
                    UtilConstants.CHOICE_KG = UtilConstants.UNIT_LB;
                    danwei_tv.setText("ft:in");
                    target_danwei_tv.setText(UtilConstants.UNIT_LB);
                    break;
            }
        }
    };

    OnClickListener sexOnClickListener = new OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.sex_male:
                    sex = "1";
                   /* if (TextUtils.isEmpty(photoImg)) {
                        ib_upphoto.setImageResource(R.drawable.nan);
                    }*/

                    maleBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.mand));
                    femaleBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.women));
                    sex_male_checkbox.setChecked(true);
                    sex_female_checkbox.setChecked(false);
                    break;
                case R.id.sex_female:
                    sex = "0";
                   /* if (TextUtils.isEmpty(photoImg)) {
                        ib_upphoto.setImageResource(R.drawable.nu);
                    }*/
                    maleBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.man));
                    femaleBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.womend));
                    sex_male_checkbox.setChecked(false);
                    sex_female_checkbox.setChecked(true);
                    break;
            }
        }
    };

    OnClickListener leverOnClickListener = new OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            switch (v.getId()) {

                case (R.id.ordinary_btn):
                    userType = "0";
                    imageView6.setImageResource(R.drawable.pcommd);
                    iv_amateur.setImageResource(R.drawable.pyeyu);
                    iv_yundong.setImageResource(R.drawable.pyundong);
                    ordinary_btn_checkbox.setChecked(true);
                    amateur_btn_checkbox.setChecked(false);
                    profess_btn_checkbox.setChecked(false);
                    showAlertDailog(getResources().getString(R.string.putong_waring_title));
                    break;

                case (R.id.amateur_btn):
                    userType = "1";
                    imageView6.setImageResource(R.drawable.pcomm);
                    iv_amateur.setImageResource(R.drawable.pyeyud);
                    iv_yundong.setImageResource(R.drawable.pyundong);
                    ordinary_btn_checkbox.setChecked(false);
                    amateur_btn_checkbox.setChecked(true);
                    profess_btn_checkbox.setChecked(false);
                    showAlertDailog(getResources().getString(R.string.yeyu_waring_title));


                    break;
                case (R.id.profess_btn):
                    userType = "2";
                    imageView6.setImageResource(R.drawable.pcomm);
                    iv_amateur.setImageResource(R.drawable.pyeyu);
                    iv_yundong.setImageResource(R.drawable.pyundongd);

                    ordinary_btn_checkbox.setChecked(false);
                    amateur_btn_checkbox.setChecked(false);
                    profess_btn_checkbox.setChecked(true);
                    showAlertDailog(getResources().getString(R.string.zhuanye_waring_title));
                    break;
            }
        }
    };

    @OnClick(R.id.userCancel)
    public void cancleClick() {
        exit();
    }

    String sex = "1";
    OnClickListener imgOnClickListener = new OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {
                case (R.id.userCancel):
                    exit();
                    break;
                case (R.id.userSave):
                    saveUser();
                    break;
                case (R.id.add_brith_day):
                    /*增加时延，防止快速点击两次*/
                    if ((System.currentTimeMillis() - ageClickTime) > 1000) {
                        showDateTimePicker();
                    }
                    ageClickTime = System.currentTimeMillis();
                    break;

            }
        }
    };

    /**
     * 退出
     */
    private void exit() {
//		if (AppData.isCheckScale) {
//			/* 是否存在用户 */
        try {
//				if (uservice.getCount() > 0) {
//					UserEditActivity.this.startActivity(new Intent(UserEditActivity.this, UserListActivity.class));
//				}else{
//					/* 结束程序 */
//					ExitApplication.getInstance().exit(UserEditActivity.this);
//				}
            if (isFirst) {
                if (null != LoadingActivity.mainActivty) {
                    LoadingActivity.mainActivty.finish();
                }
                System.exit(0);
                BabyUserEditActivity.this.finish();
            } else {
                if (null != user && user.getId() > 0) {
                    Message msg = saveHandler.obtainMessage(UPDATE_ATION);
                    saveHandler.sendMessage(msg);
                } else {
                    BabyUserEditActivity.this.finish();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 判断
     */
    private void saveUser() {
        String sName = nameET.getText().toString();
        if (null == sName || "".equals(sName.trim())) {
            Toast.makeText(this, getString(R.string.name_error), Toast.LENGTH_SHORT).show();
            return;
        }
        String ageday = ageET.getText().toString();
        if (null == ageday || "".equals(ageday.trim())) {
            Toast.makeText(this, getString(R.string.age_error), Toast.LENGTH_SHORT).show();
            return;
        }

        String year = ageday.substring(ageday.lastIndexOf("-") + 1);
        String mm = ageday.substring(0, ageday.lastIndexOf("-"));
        ageday = year + "-" + mm;
        int age = Tool.getAgeByBirthday(Tool.StringToDate(ageday, "yyyy-MM-dd"));
        if (age < 1 || age > 10) {
            Toast.makeText(this, getString(R.string.age_error_10), Toast.LENGTH_SHORT).show();
            return;
        }


        if (isKG) {
            String height1 = heightET.getText().toString();
            if (null == height1 || "".equals(height1.trim())) {
                Toast.makeText(this, getString(R.string.height_error_3), Toast.LENGTH_LONG)
                        .show();
                return;
            }
            if (Float.parseFloat(height1) < 30 || Float.parseFloat(height1) > 220) {
                Toast.makeText(this, getString(R.string.height_error_3), Toast.LENGTH_LONG)
                        .show();
                return;
            }

        } else {
            // heightET.setText(UtilTooth.myround(UtilTooth.cm2foot(user2.getBheigth()))+"");
            String h1 = heightET.getText().toString();
            if ("".equals(h1.trim()))
                h1 = "0";
            String h2 = heightET2.getText().toString();
            if ("".equals(h2.trim()))
                h2 = "0";
            int h11 = (int) Float.parseFloat(h1);
            int h22 = (int) Float.parseFloat(h2);
            if ((h11 < 1) || (h11 > 7) || (h11 == 7 && h22 > 3)) {
                Toast.makeText(this, getString(R.string.height_error_4), Toast.LENGTH_LONG)
                        .show();
                return;
            }
        }
        if (null == userType || "".equals(userType.trim())) {
            Toast.makeText(this, getString(R.string.user_info_level_need), Toast.LENGTH_SHORT).show();
            return;
        }

        Message msg = saveHandler.obtainMessage(SAVE_ATION);
        saveHandler.sendMessage(msg);
    }


    public static final int SAVE_ATION = 1;
    public static final int UPDATE_ATION = 2;
    private final Handler saveHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SAVE_ATION:
                    if (null != user) {
                        /* 判断是否是识别界面进入 */
                        UserModel user = creatUserModel();

                        if (user.getId() > 0) {
                            /* 保存用户信息 */
                            try {
                                if (UtilConstants.CURRENT_SCALE.equals(UtilConstants.KITCHEN_SCALE)) {
                                    user.setDanwei(UtilConstants.UNIT_KG);
                                }
                                uservice.update(user);
                                user = uservice.find(user.getId());
                                UtilConstants.SELECT_USER = user.getId();
                                Intent intent = new Intent();
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("user", user);
                                intent.putExtras(bundle);
                                setResult(RESULT_OK, intent);
                                if ("P999".equals(user.getGroup())) {
                                    EventBus.getDefault().post(new ReFlushBabyEvent(user));
                                }
                                BabyUserEditActivity.this.finish();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        } else {
                            /*跳转到指定的扫描界面*/
                            int currentapiVersion = Build.VERSION.SDK_INT;
                            Intent intent1 = new Intent();
                            intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            if (currentapiVersion < 18) {
                            } else {
                                intent1.setClass(BabyUserEditActivity.this, AutoBLEActivity.class);
                            }
                            BabyUserEditActivity.this.startActivity(intent1);
                            finish();
                        }
                    }
                    break;
                case UPDATE_ATION:
                    if (null != user) {
                        /* 判断是否是识别界面进入 */


                        if (user.getId() > 0) {
                            /* 保存用户信息 */
                            try {
                                if (UtilConstants.CURRENT_SCALE.equals(UtilConstants.KITCHEN_SCALE)) {
                                    user.setDanwei(UtilConstants.UNIT_KG);
                                }
                                uservice.update(user);
                                user = uservice.find(user.getId());
                                UtilConstants.SELECT_USER = user.getId();
                                Intent intent = new Intent();
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("user", user);
                                intent.putExtras(bundle);
                                setResult(RESULT_OK, intent);
                                if ("P999".equals(user.getGroup())) {
                                    EventBus.getDefault().post(new ReFlushBabyEvent(user));
                                }
                                BabyUserEditActivity.this.finish();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        } else {
							/*跳转到指定的扫描界面*/
                            int currentapiVersion = Build.VERSION.SDK_INT;
                            Intent intent1 = new Intent();
                            intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            if (currentapiVersion < 18) {
                            } else {
                                intent1.setClass(BabyUserEditActivity.this, AutoBLEActivity.class);
                            }
                            BabyUserEditActivity.this.startActivity(intent1);
                            finish();
                        }
                    }
                    break;
            }
        }
    };

    /**
     * 创建用户
     */
    public UserModel creatUserModel() {
        if (null != user) {
            user.setLevel(userType);
            user.setUserName(nameET.getText().toString());
            user.setScaleType(UtilConstants.CURRENT_SCALE);
            user.setDanwei(UtilConstants.CHOICE_KG);
            if (UtilConstants.CURRENT_SCALE.equals(UtilConstants.BABY_SCALE)) {
                if (UtilConstants.CHOICE_KG.equals(UtilConstants.UNIT_LB) || UtilConstants.CHOICE_KG.equals(UtilConstants.UNIT_ST) || UtilConstants.CHOICE_KG.equals(UtilConstants.UNIT_FATLB)) {
                    user.setDanwei(UtilConstants.UNIT_FATLB);
                } else {
                    user.setDanwei(UtilConstants.UNIT_KG);
                }
            }
            user.setPer_photo(photoImg);
            user.setSex(sex);
            String hei = this.heightET.getText().toString().trim();
            String hei2 = this.heightET2.getText().toString().trim();
            if (null == hei || "".equals(hei)) {
                hei = "0";
            }
            if (null == hei2 || "".equals(hei2)) {
                hei2 = "0";
            }
            String tage = this.target_edittv.getText().toString().trim();
            String tage2 = this.target_edittv2.getText().toString().trim();
            if (null == tage || "".equals(tage)) {
                tage = "0";
            }
            if (null == tage2 || "".equals(tage2)) {
                tage2 = "0";
            }
            String age = this.ageET.getText().toString().trim();
            if (null == age || "".equals(age)) {
                age = "0";
            }
            if (null != monthET) {
                String mont = monthET.getText().toString();
                if ("".equals(mont.trim())) {
                    user.setAgeMonth(0);
                } else {
                    user.setAgeMonth(Integer.parseInt(mont));
                }
            }
            if (null != target_edittv) {
                String wei = target_edittv.getText().toString().trim();
                if (null == wei || "".equals(wei)) {
                    wei = "0";
                }
                float tg = Float.parseFloat(wei);
                if (tg < 1)
                    tg = 0f;
                if (UtilConstants.CHOICE_KG.equals(UtilConstants.UNIT_KG)) {
                    user.setTargweight(tg);
                } else {
                    user.setTargweight(UtilTooth.lbToKg_target(tg));
                }
            }
            if (isKG) {
                user.setBheigth(Float.parseFloat(hei));
            } else {
                user.setBheigth(UtilTooth.ft_in2CMArray(new String[]{hei, hei2}));
            }
            if (isKG) {
                user.setBheigth(Float.parseFloat(hei));
            } else {
                user.setBheigth(UtilTooth.ft_in2CMArray(new String[]{hei, hei2}));
            }
            su.editSharedPreferences("lefuconfig", "unit", UtilConstants.CHOICE_KG);
            user.setBirth(age);
            if (null != age && !"".equals(age)) {
                if (age.lastIndexOf("-") > 0) {
                    String year = age.substring(age.lastIndexOf("-") + 1);
                    String mm = age.substring(0, age.lastIndexOf("-"));
                    age = year + "-" + mm;
                }
            }
            user.setAgeYear(Tool.getAgeByBirthday(Tool.StringToDate(age, "yyyy-MM-dd")));
        }

        return user;
    }

    private WheelMain wheelMain;
    /***
     * 自定义Dialog
     */
    private AlertDialog dialog;
    /**
     * 是否成功保存
     **/
    private boolean isOK;

    /**
     * 时间滚动器
     */
    public void showDateTimePicker() {
        LayoutInflater inflater = LayoutInflater.from(BabyUserEditActivity.this);
        View timepickerview = inflater.inflate(R.layout.datewheelpick, null);
        timepickerview.setMinimumWidth(getWindowManager().getDefaultDisplay().getWidth());
        ScreenInfo screenInfo = new ScreenInfo(BabyUserEditActivity.this);
        wheelMain = new WheelMain(timepickerview);
        wheelMain.screenheight = screenInfo.getHeight();
        Calendar calendar = Calendar.getInstance();
        // calendar.setTime(dateFormat.parse(time));设置指定时间
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        wheelMain.setTime(year, month, day);
        dialog = new AlertDialog.Builder(this).setView(timepickerview).show();

        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
        window.setWindowAnimations(R.style.mystyle); // 添加动画

        Button btn = (Button) timepickerview.findViewById(R.id.btn_datetime_sure);
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                isOK = true;
                ageET.setText(wheelMain.getTime());
                dialog.dismiss();
            }
        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            exit();
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_HOME) {
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    OnClickListener photoClickListener = new OnClickListener() {
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.reviseHead) {
                showSetPhotoDialog();
            } else if (id == R.id.rb_setPhoto1) {
                alertDialog.dismiss();
                customHelper.selctImage(getTakePhoto(), 2);
            } else if (id == R.id.rb_setPhoto2) {
                alertDialog.dismiss();
                customHelper.selctImage(getTakePhoto(), 1);
            } else if (id == R.id.rb_setPhoto3) {
                alertDialog.dismiss();
            }
        }
    };

    @Override
    public void takeCancel() {
        super.takeCancel();

    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        showImg(result.getImages());
    }

    private void showImg(final ArrayList<TImage> images) {
        photoImg = images.get(0).getCompressPath();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ib_upphoto.setImageURI(Uri.fromFile(new File
                        (photoImg)));

            }
        });
    }
}

